// ChessController.h
//
// Created By: Jeremy Butler
// Created On: 23-Mar-2011
//
// A chess game controller that keeps track of turn taking
// and updating of the board model and the GUI.

#ifndef CHESS_CONTROLLER_H
#define CHESS_CONTROLLER_H

#include "IChessController.h"
#include "Game.h"
#include "ChessPlayer.h"
#include "ChessView.h"
#include "ChessUtils.h"
using namespace std;

class ChessController : public IChessController {
  friend class ComputerPlayer;
  friend bool Pawn::Test(ostream & os);
  friend bool Rook::Test(ostream & os);
  friend bool Bishop::Test(ostream & os);
  friend bool Knight::Test(ostream & os);
  friend bool Queen::Test(ostream & os);
  friend bool King::Test(ostream & os);

private:
  IChessView * view; //The GUI
  Game * model; //The Model
  string fileName; //The name of this saved chess game.
  ChessPlayer * playerWhite; //A representation of the White chess player.
  ChessPlayer * playerBlack; //A representation of the Black chess player.
  ChessPlayer * currentPlayer;
  bool gameOver;
  
public:
  //! @brief No-Arg Constructor
  ChessController();

  //! @brief Contructor
  ChessController(ChessView & _view, int gameType);

  //! @brief Copy Constructor
  //! @param other the ChessController to copy.
  ChessController(const ChessController & other);

  //! @brief Operator = to copy a ChessController to this one.
  //! @param other The ChessController to copy.
  //! @return a reference to the newly copied ChessController
  ChessController & operator = (const ChessController & other);

  //! @brief Destrcutor
  ~ChessController();

  // Begin Interface Functions
  virtual void on_CellSelected(int row, int col, int button);
  virtual bool on_DragEnd(int row, int col);
  virtual void on_DragStart(int row, int col);
  virtual void on_LoadGame();
  virtual void on_NewGame();
  virtual void on_QuitGame();
  virtual void on_SaveGame();
  virtual void on_SaveGameAs();
  virtual void on_TimerEvent();
  virtual void on_UndoMove();
  virtual void SetView(IChessView* view);
  // End Interface Functions
  
private:
  void LoadGame(const string & fileName);
  void SaveGame(const string & fileName);
  ImageName GetImageName(const Piece & piece);
  void HighlightValidMoves(const set<Location> & validMoves, int row, int col);
  void UnhighlightAll();
  void UpdateGameState();
  void ResetView();
  void PrepareNextTurn();
};

#endif /* CHESS_CONTROLLER_H */
